﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Xml;
using System.Xml.Linq;
using WDTAsg2.DbBizObject;


namespace WDTAsg2.Member
{
    public partial class Downloads : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

            // Code modified from http://msdn.microsoft.com/en-us/library/ms227669.aspx


            if (IsPostBack)
            {
                Boolean fileOK = false;
                String path = Server.MapPath("~/Drivers/");
                if (FileUpload1.HasFile)
                {
                    String fileExtension =
                        System.IO.Path.GetExtension(FileUpload1.FileName).ToLower();
                    String[] allowedExtensions = { ".txt" };
                    for (int i = 0; i < allowedExtensions.Length; i++)
                    {
                        if (fileExtension == allowedExtensions[i])
                        {
                            fileOK = true;
                        }
                    }
                }

                if (fileOK)
                {
                    try
                    {

                        FileUpload1.PostedFile.SaveAs(path
                            + FileUpload1.FileName);
                        lblUpload.Text = "File uploaded!";


                        CustomProfile profile = CustomProfile.GetProfile();

                        // Insert the download into the database with the user who uploaded it
                        DownloadInfo.InsertDownload(txtDriverTitle.Text, txtDriverDesc.Text,
                            Convert.ToInt32(ddlDriverCat.SelectedValue),
                            "/Drivers/" + FileUpload1.FileName, profile.UserName);


                    }
                    catch (Exception ex)
                    {
                        lblUpload.Text = "File could not be uploaded.";
                    }
                }
                else
                {
                    lblUpload.Text = "Cannot accept files of this type.";
                }
            }


            //First time loading
            if (!Page.IsPostBack)
            {

                //binding data to the gridview
                //lvDownloads.DataSource = DownloadInfo.GetAllDownloads();
                //lvDownloads.DataBind();

                //binding data to the first DropDownList
                dDListPCat.DataSource = CategoryInfo.GetParentCategories();
                dDListPCat.DataBind();

                dDListPCat.Items.Insert(0, new ListItem("All", "All"));
                dDListPCat.SelectedValue = "All";



                // Download dropdown list
                //binding data to the first DropDownList
                ddlDriverPCat.DataSource = CategoryInfo.GetParentCategories();
                ddlDriverPCat.DataBind();

                ddlDriverPCat.Items.Insert(0, new ListItem("All", "All"));
                ddlDriverPCat.SelectedValue = "All";

            }
        }


        protected void dDListPCat_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (dDListPCat.SelectedValue == "All")
            {
                //binding data to the gridview
                lvDownloads.DataSource = DownloadInfo.GetAllApprovedDownloads();
                lvDownloads.DataBind();

                //binding data to the first DropDownList
                dDListPCat.DataSource = CategoryInfo.GetParentCategories();
                dDListPCat.DataBind();

                dDListPCat.Items.Insert(0, new ListItem("All", "All"));
                dDListPCat.SelectedValue = "All";

                // Hide drop down lists
                dDListCat.Visible = false;
                dDListModel.Visible = false;

                return;
            }

            // Update grid view
            lvDownloads.DataSource = DownloadInfo.GetDownloadsByCategory(Convert.ToInt32(dDListPCat.SelectedValue));
            lvDownloads.DataBind();

            // Update model drop down
            dDListCat.DataSource = CategoryInfo.GetChildCategories(Convert.ToInt32(dDListPCat.SelectedValue));
            dDListCat.DataBind();

            dDListCat.Items.Insert(0, new ListItem("All", "All"));
            dDListCat.SelectedValue = "All";
            dDListCat.Visible = true;

            dDListModel.Visible = false;
        }


        protected void dDListCat_SelectedIndexChanged(object sender, EventArgs e)
        {


            if (dDListCat.SelectedValue == "All")
            {
                dDListPCat_SelectedIndexChanged(sender, e);
                return;
            }


            // Update grid view
            lvDownloads.DataSource = DownloadInfo.GetDownloadsByCategory(Convert.ToInt32(dDListCat.SelectedValue));
            lvDownloads.DataBind();

            // Update model drop down
            dDListModel.DataSource = DownloadInfo.GetDownloadsByCategory(Convert.ToInt32(dDListCat.SelectedValue));
            dDListModel.DataBind();

            dDListModel.Items.Insert(0, new ListItem("All", "All"));
            dDListModel.SelectedValue = "All";
            dDListModel.Visible = true;

        }


        protected void dDListModel_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (dDListModel.SelectedValue == "All")
            {
                dDListCat_SelectedIndexChanged(sender, e);
                return;
            }

            // Update grid view
            lvDownloads.DataSource = DownloadInfo.GetDownloadById(Convert.ToInt32(dDListModel.SelectedValue));
            lvDownloads.DataBind();
        }


        protected void ddlDriverPCat_SelectedIndexChanged(object sender, System.EventArgs e)
        {


            if (ddlDriverPCat.SelectedValue == "All")
            {

                //binding data to the first DropDownList
                dDListPCat.DataSource = CategoryInfo.GetParentCategories();
                dDListPCat.DataBind();

                dDListPCat.Items.Insert(0, new ListItem("All", "All"));
                dDListPCat.SelectedValue = "All";

                // Hide drop down lists
                ddlDriverCat.Visible = false;
                return;
            }



            // Update sub-cat drop down
            ddlDriverCat.DataSource = CategoryInfo.GetChildCategories(Convert.ToInt32(ddlDriverPCat.SelectedValue));
            ddlDriverCat.DataBind();

            ddlDriverCat.Items.Insert(0, new ListItem("All", "All"));
            ddlDriverCat.SelectedValue = "All";
            ddlDriverCat.Visible = true;


        }

        protected void ddlDriverCat_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            // If they select all reset the fields
            if (ddlDriverCat.SelectedValue == "All")
            {
                ddlDriverPCat_SelectedIndexChanged(sender, e);
                return;
            }
        }


    }
}